<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      Go语言之fmt | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>Go语言之fmt</h2>



  <p class="post-date">2019-03-10</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><p>fmt标准库是我们在学习Go语言过程中接触最早最频繁的一个了，本文介绍了fmtb包的一些常用函数。</p>

<h1 id="fmt"><a href="#fmt" class="headerlink" title="fmt"></a>fmt</h1><p>fmt包实现了类似C语言printf和scanf的格式化I/O。主要分为向外输出内容和获取输入内容两大部分。</p>

<h1 id="向外输出"><a href="#向外输出" class="headerlink" title="向外输出"></a>向外输出</h1><p>标准库<code>fmt</code>提供了以下几种输出相关函数。</p>

<h1 id="Print"><a href="#Print" class="headerlink" title="Print"></a>Print</h1><p><code>Print</code>系列函数会将内容输出到系统的标准输出，区别在于<code>Print</code>函数直接输出内容，<code>Printf</code>函数支持格式化输出字符串，<code>Println</code>函数会在输出内容的结尾添加一个换行符。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Print</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Printf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Println</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<p>举个简单的例子：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Print(&amp;quot;在终端打印该信息。&amp;quot;)</span><br><span class="line">	name := &amp;quot;沙河小王子&amp;quot;</span><br><span class="line">	fmt.Printf(&amp;quot;我是：%s\n&amp;quot;, name)</span><br><span class="line">	fmt.Println(&amp;quot;在终端打印单独一行显示&amp;quot;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行上面的代码输出：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在终端打印该信息。我是：沙河小王子</span><br><span class="line">在终端打印单独一行显示</span><br></pre></td></tr></table></figure>

<h1 id="Fprint"><a href="#Fprint" class="headerlink" title="Fprint"></a>Fprint</h1><p><code>Fprint</code>系列函数会将内容输出到一个<code>io.Writer</code>接口类型的变量<code>w</code>中，我们通常用这个函数往文件中写入内容。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprint</span><span class="params">(w io.Writer, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprintf</span><span class="params">(w io.Writer, format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprintln</span><span class="params">(w io.Writer, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<p>举个例子：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 向标准输出写入内容</span></span><br><span class="line">fmt.Fprintln(os.Stdout, &amp;quot;向标准输出写入内容&amp;quot;)</span><br><span class="line">fileObj, err := os.OpenFile(&amp;quot;./xx.txt&amp;quot;, os.O_CREATE|os.O_WRONLY|os.O_APPEND, <span class="number">0644</span>)</span><br><span class="line"><span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">	fmt.Println(&amp;quot;打开文件出错，err:&amp;quot;, err)</span><br><span class="line">	<span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line">name := &amp;quot;沙河小王子&amp;quot;</span><br><span class="line"><span class="comment">// 向打开的文件句柄中写入内容</span></span><br><span class="line">fmt.Fprintf(fileObj, &amp;quot;往文件中写如信息：%s&amp;quot;, name)</span><br></pre></td></tr></table></figure>

<p>注意，只要满足<code>io.Writer</code>接口的类型都支持写入。</p>

<h1 id="Sprint"><a href="#Sprint" class="headerlink" title="Sprint"></a>Sprint</h1><p><code>Sprint</code>系列函数会把传入的数据生成并返回一个字符串。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprint</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprintf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprintln</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br></pre></td></tr></table></figure>

<p>简单的示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">s1 := fmt.Sprint(&amp;quot;沙河小王子&amp;quot;)</span><br><span class="line">name := &amp;quot;沙河小王子&amp;quot;</span><br><span class="line">age := <span class="number">18</span></span><br><span class="line">s2 := fmt.Sprintf(&amp;quot;name:%s,age:%d&amp;quot;, name, age)</span><br><span class="line">s3 := fmt.Sprintln(&amp;quot;沙河小王子&amp;quot;)</span><br><span class="line">fmt.Println(s1, s2, s3)</span><br></pre></td></tr></table></figure>

<h1 id="Errorf"><a href="#Errorf" class="headerlink" title="Errorf"></a>Errorf</h1><p><code>Errorf</code>函数根据format参数生成格式化字符串并返回一个包含该字符串的错误。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Errorf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">error</span></span></span><br></pre></td></tr></table></figure>

<p>通常使用这种方式来自定义错误类型，例如：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">err := fmt.Errorf(&amp;quot;这是一个错误&amp;quot;)</span><br></pre></td></tr></table></figure>

<h1 id="格式化占位符"><a href="#格式化占位符" class="headerlink" title="格式化占位符"></a>格式化占位符</h1><p><code>*printf</code>系列函数都支持format格式化参数，在这里我们按照占位符将被替换的变量类型划分，方便查询和记忆。</p>

<h1 id="通用占位符"><a href="#通用占位符" class="headerlink" title="通用占位符"></a>通用占位符</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%v</td>
<td>值的默认格式表示</td>
</tr>

<tr>
<td>%+v</td>
<td>类似%v，但输出结构体时会添加字段名</td>
</tr>

<tr>
<td>%#v</td>
<td>值的Go语法表示</td>
</tr>

<tr>
<td>%T</td>
<td>打印值的类型</td>
</tr>

<tr>
<td>%%</td>
<td>百分号</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">fmt.Printf(&amp;quot;%v\n&amp;quot;, <span class="number">100</span>)</span><br><span class="line">fmt.Printf(&amp;quot;%v\n&amp;quot;, <span class="literal">false</span>)</span><br><span class="line">o := <span class="keyword">struct</span>&#123; name <span class="keyword">string</span> &#125;&#123;&amp;quot;小王子&amp;quot;&#125;</span><br><span class="line">fmt.Printf(&amp;quot;%v\n&amp;quot;, o)</span><br><span class="line">fmt.Printf(&amp;quot;%#v\n&amp;quot;, o)</span><br><span class="line">fmt.Printf(&amp;quot;%T\n&amp;quot;, o)</span><br><span class="line">fmt.Printf(&amp;quot;<span class="number">100</span>%%\n&amp;quot;)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">100</span><br><span class="line"><span class="literal">false</span></span><br><span class="line">&#123;小王子&#125;</span><br><span class="line">struct &#123; name string &#125;&#123;name:&amp;quot;小王子&amp;quot;&#125;</span><br><span class="line">struct &#123; name string &#125;</span><br><span class="line">100%</span><br></pre></td></tr></table></figure>

<h1 id="布尔型"><a href="#布尔型" class="headerlink" title="布尔型"></a>布尔型</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%t</td>
<td>true或false</td>
</tr>
</tbody>
</table>

<h1 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%b</td>
<td>表示为二进制</td>
</tr>

<tr>
<td>%c</td>
<td>该值对应的unicode码值</td>
</tr>

<tr>
<td>%d</td>
<td>表示为十进制</td>
</tr>

<tr>
<td>%o</td>
<td>表示为八进制</td>
</tr>

<tr>
<td>%x</td>
<td>表示为十六进制，使用a-f</td>
</tr>

<tr>
<td>%X</td>
<td>表示为十六进制，使用A-F</td>
</tr>

<tr>
<td>%U</td>
<td>表示为Unicode格式：U+1234，等价于&rdquo;U+%04X&rdquo;</td>
</tr>

<tr>
<td>%q</td>
<td>该值对应的单引号括起来的go语法字符字面值，必要时会采用安全的转义表示</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">n := <span class="number">65</span></span><br><span class="line">fmt.Printf(&amp;quot;%b\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%c\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%d\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%o\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%x\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%X\n&amp;quot;, n)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1000001</span><br><span class="line">A</span><br><span class="line">65</span><br><span class="line">101</span><br><span class="line">41</span><br><span class="line">41</span><br></pre></td></tr></table></figure>

<h1 id="浮点数与复数"><a href="#浮点数与复数" class="headerlink" title="浮点数与复数"></a>浮点数与复数</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%b</td>
<td>无小数部分、二进制指数的科学计数法，如-123456p-78</td>
</tr>

<tr>
<td>%e</td>
<td>科学计数法，如-1234.456e+78</td>
</tr>

<tr>
<td>%E</td>
<td>科学计数法，如-1234.456E+78</td>
</tr>

<tr>
<td>%f</td>
<td>有小数部分但无指数部分，如123.456</td>
</tr>

<tr>
<td>%F</td>
<td>等价于%f</td>
</tr>

<tr>
<td>%g</td>
<td>根据实际情况采用%e或%f格式（以获得更简洁、准确的输出）</td>
</tr>

<tr>
<td>%G</td>
<td>根据实际情况采用%E或%F格式（以获得更简洁、准确的输出）</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">f := <span class="number">12.34</span></span><br><span class="line">fmt.Printf(&amp;quot;%b\n&amp;quot;, f)</span><br><span class="line">fmt.Printf(&amp;quot;%e\n&amp;quot;, f)</span><br><span class="line">fmt.Printf(&amp;quot;%E\n&amp;quot;, f)</span><br><span class="line">fmt.Printf(&amp;quot;%f\n&amp;quot;, f)</span><br><span class="line">fmt.Printf(&amp;quot;%g\n&amp;quot;, f)</span><br><span class="line">fmt.Printf(&amp;quot;%G\n&amp;quot;, f)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">6946802425218990p-49</span><br><span class="line">1.234000e+01</span><br><span class="line">1.234000E+01</span><br><span class="line">12.340000</span><br><span class="line">12.34</span><br><span class="line">12.34</span><br></pre></td></tr></table></figure>

<h1 id="字符串和-byte"><a href="#字符串和-byte" class="headerlink" title="字符串和[]byte"></a>字符串和[]byte</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%s</td>
<td>直接输出字符串或者[]byte</td>
</tr>

<tr>
<td>%q</td>
<td>该值对应的双引号括起来的go语法字符串字面值，必要时会采用安全的转义表示</td>
</tr>

<tr>
<td>%x</td>
<td>每个字节用两字符十六进制数表示（使用a-f</td>
</tr>

<tr>
<td>%X</td>
<td>每个字节用两字符十六进制数表示（使用A-F）</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s := &amp;quot;小王子&amp;quot;</span><br><span class="line">fmt.Printf(&amp;quot;%s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%q\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%x\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%X\n&amp;quot;, s)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">小王子</span><br><span class="line">&amp;quot;小王子&amp;quot;</span><br><span class="line">e5b08fe78e8be5ad90</span><br><span class="line">E5B08FE78E8BE5AD90</span><br></pre></td></tr></table></figure>

<h1 id="指针"><a href="#指针" class="headerlink" title="指针"></a>指针</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%p</td>
<td>表示为十六进制，并加上前导的0x</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a := <span class="number">10</span></span><br><span class="line">fmt.Printf(&amp;quot;%p\n&amp;quot;, &amp;amp;a)</span><br><span class="line">fmt.Printf(&amp;quot;%#p\n&amp;quot;, &amp;amp;a)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0xc000094000</span><br><span class="line">c000094000</span><br></pre></td></tr></table></figure>

<h1 id="宽度标识符"><a href="#宽度标识符" class="headerlink" title="宽度标识符"></a>宽度标识符</h1><p>宽度通过一个紧跟在百分号后面的十进制数指定，如果未指定宽度，则表示值时除必需之外不作填充。精度通过（可选的）宽度后跟点号后跟的十进制数指定。如果未指定精度，会使用默认精度；如果点号后没有跟数字，表示精度为0。举例如下：</p>

<table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>%f</td>
<td>默认宽度，默认精度</td>
</tr>

<tr>
<td>%9f</td>
<td>宽度9，默认精度</td>
</tr>

<tr>
<td>%.2f</td>
<td>默认宽度，精度2</td>
</tr>

<tr>
<td>%9.2f</td>
<td>宽度9，精度2</td>
</tr>

<tr>
<td>%9.f</td>
<td>宽度9，精度0</td>
</tr>
</tbody>
</table>

<p>示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">n := <span class="number">12.34</span></span><br><span class="line">fmt.Printf(&amp;quot;%f\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">9</span>f\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">.2</span>f\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">9.2</span>f\n&amp;quot;, n)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">9.</span>f\n&amp;quot;, n)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">12.340000</span><br><span class="line">12.340000</span><br><span class="line">12.34</span><br><span class="line">    12.34</span><br><span class="line">       12</span><br></pre></td></tr></table></figure>

<h1 id="其他falg"><a href="#其他falg" class="headerlink" title="其他falg"></a>其他falg</h1><table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>

<tbody>
<tr>
<td>&rsquo;+&rsquo;</td>
<td>总是输出数值的正负号；对%q（%+q）会生成全部是ASCII字符的输出（通过转义）；</td>
</tr>

<tr>
<td>&rsquo; &lsquo;</td>
<td>对数值，正数前加空格而负数前加负号；对字符串采用%x或%X时（% x或% X）会给各打印的字节之间加空格</td>
</tr>

<tr>
<td>&rsquo;-&rsquo;</td>
<td>在输出右边填充空白而不是默认的左边（即从默认的右对齐切换为左对齐）；</td>
</tr>

<tr>
<td>&rsquo;#&rsquo;</td>
<td>八进制数前加0（%#o），十六进制数前加0x（%#x）或0X（%#X），指针去掉前面的0x（%#p）对%q（%#q），对%U（%#U）会输出空格和单引号括起来的go字面值；</td>
</tr>

<tr>
<td>&lsquo;0&rsquo;</td>
<td>使用0而不是空格填充，对于数值类型会把填充的0放在正负号后面；</td>
</tr>
</tbody>
</table>

<p>举个例子：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">s := &amp;quot;小王子&amp;quot;</span><br><span class="line">fmt.Printf(&amp;quot;%s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">5</span>s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">-5</span>s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">5.7</span>s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">-5.7</span>s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">5.2</span>s\n&amp;quot;, s)</span><br><span class="line">fmt.Printf(&amp;quot;%<span class="number">05</span>s\n&amp;quot;, s)</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">小王子</span><br><span class="line">  小王子</span><br><span class="line">小王子  </span><br><span class="line">  小王子</span><br><span class="line">小王子  </span><br><span class="line">   小王</span><br><span class="line">00小王子</span><br></pre></td></tr></table></figure>

<h1 id="获取输入"><a href="#获取输入" class="headerlink" title="获取输入"></a>获取输入</h1><p>Go语言<code>fmt</code>包下有<code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，可以在程序运行过程中从标准输入获取用户的输入。</p>

<h1 id="fmt-Scan"><a href="#fmt-Scan" class="headerlink" title="fmt.Scan"></a>fmt.Scan</h1><p>函数定签名如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scan</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>Scan从标准输入扫描文本，读取由空白符分隔的值保存到传递给本函数的参数中，换行符视为空白符。</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误。如果读取的数据个数比提供的参数少，会返回一个错误报告原因。</li>
</ul>

<p>具体代码示例如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scan(&amp;amp;name, &amp;amp;age, &amp;amp;married)</span><br><span class="line">	fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端依次输入<code>小王子</code>、<code>28</code>和<code>false</code>使用空格分隔。</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">小王子 28 <span class="literal">false</span></span><br><span class="line">扫描结果 name:小王子 age:28 married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scan</code>从标准输入中扫描用户输入的数据，将以空白符分隔的数据分别存入指定的参数。</p>

<h1 id="fmt-Scanf"><a href="#fmt-Scanf" class="headerlink" title="fmt.Scanf"></a>fmt.Scanf</h1><p>函数签名如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scanf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>Scanf从标准输入扫描文本，根据format参数指定的格式去读取由空白符分隔的值保存到传递给本函数的参数中。</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误。</li>
</ul>

<p>代码示例如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanf(&amp;quot;<span class="number">1</span>:%s <span class="number">2</span>:%d <span class="number">3</span>:%t&amp;quot;, &amp;amp;name, &amp;amp;age, &amp;amp;married)</span><br><span class="line">	fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端按照指定的格式依次输入<code>小王子</code>、<code>28</code>和<code>false</code>。</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">1:小王子 2:28 3:<span class="literal">false</span></span><br><span class="line">扫描结果 name:小王子 age:28 married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scanf</code>不同于<code>fmt.Scan</code>简单的以空格作为输入数据的分隔符，<code>fmt.Scanf</code>为输入数据指定了具体的输入内容格式，只有按照格式输入数据才会被扫描并存入对应变量。</p>

<p>例如，我们还是按照上个示例中以空格分隔的方式输入，<code>fmt.Scanf</code>就不能正确扫描到输入的数据。</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">小王子 28 <span class="literal">false</span></span><br><span class="line">扫描结果 name: age:0 married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<h1 id="fmt-Scanln"><a href="#fmt-Scanln" class="headerlink" title="fmt.Scanln"></a>fmt.Scanln</h1><p>函数签名如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scanln</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>Scanln类似Scan，它在遇到换行时才停止扫描。最后一个数据后面必须有换行或者到达结束位置。</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误。</li>
</ul>

<p>具体代码示例如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanln(&amp;amp;name, &amp;amp;age, &amp;amp;married)</span><br><span class="line">	fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端依次输入<code>小王子</code>、<code>28</code>和<code>false</code>使用空格分隔。</p>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">小王子 28 <span class="literal">false</span></span><br><span class="line">扫描结果 name:小王子 age:28 married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scanln</code>遇到回车就结束扫描了，这个比较常用。</p>

<h1 id="bufio-NewReader"><a href="#bufio-NewReader" class="headerlink" title="bufio.NewReader"></a>bufio.NewReader</h1><p>有时候我们想完整获取输入的内容，而输入的内容可能包含空格，这种情况下可以使用<code>bufio</code>包来实现。示例代码如下：</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">bufioDemo</span><span class="params">()</span></span> &#123;</span><br><span class="line">	reader := bufio.NewReader(os.Stdin) <span class="comment">// 从标准输入生成读对象</span></span><br><span class="line">	fmt.Print(&amp;quot;请输入内容：&amp;quot;)</span><br><span class="line">	text, _ := reader.ReadString(<span class="string">'\n'</span>) <span class="comment">// 读到换行</span></span><br><span class="line">	text = strings.TrimSpace(text)</span><br><span class="line">	fmt.Printf(&amp;quot;%#v\n&amp;quot;, text)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Fscan系列"><a href="#Fscan系列" class="headerlink" title="Fscan系列"></a>Fscan系列</h1><p>这几个函数功能分别类似于<code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，只不过它们不是从标准输入中读取数据而是从<code>io.Reader</code>中读取数据。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscan</span><span class="params">(r io.Reader, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscanln</span><span class="params">(r io.Reader, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscanf</span><span class="params">(r io.Reader, format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<h1 id="Sscan系列"><a href="#Sscan系列" class="headerlink" title="Sscan系列"></a>Sscan系列</h1><p>这几个函数功能分别类似于<code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，只不过它们不是从标准输入中读取数据而是从指定字符串中读取数据。</p>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscan</span><span class="params">(str <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscanln</span><span class="params">(str <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscanf</span><span class="params">(str <span class="keyword">string</span>, format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Go" >
    <span class="tag-code">Go</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/04-%E7%B1%BB%E5%9E%8B/">
        <span class="nav-arrow">← </span>
        
          4-类型
        
      </a>
    
    
      <a class="nav-right" href="/go/7-%E5%85%B6%E4%BB%96/03-Go%E5%8C%85%E7%AE%A1%E7%90%86%E8%AF%A6%E8%A7%A3/">
        
          Go包管理
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#fmt"><span class="toc-nav-text">fmt</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#向外输出"><span class="toc-nav-text">向外输出</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Print"><span class="toc-nav-text">Print</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Fprint"><span class="toc-nav-text">Fprint</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Sprint"><span class="toc-nav-text">Sprint</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Errorf"><span class="toc-nav-text">Errorf</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#格式化占位符"><span class="toc-nav-text">格式化占位符</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#通用占位符"><span class="toc-nav-text">通用占位符</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#布尔型"><span class="toc-nav-text">布尔型</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#整型"><span class="toc-nav-text">整型</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#浮点数与复数"><span class="toc-nav-text">浮点数与复数</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#字符串和-byte"><span class="toc-nav-text">字符串和[]byte</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#指针"><span class="toc-nav-text">指针</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#宽度标识符"><span class="toc-nav-text">宽度标识符</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#其他falg"><span class="toc-nav-text">其他falg</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#获取输入"><span class="toc-nav-text">获取输入</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#fmt-Scan"><span class="toc-nav-text">fmt.Scan</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#fmt-Scanf"><span class="toc-nav-text">fmt.Scanf</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#fmt-Scanln"><span class="toc-nav-text">fmt.Scanln</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#bufio-NewReader"><span class="toc-nav-text">bufio.NewReader</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Fscan系列"><span class="toc-nav-text">Fscan系列</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Sscan系列"><span class="toc-nav-text">Sscan系列</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/go/3-Go语言常用标准库/03-Go语言之fmt/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "Go语言之fmt",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>